package com.ygq.exception;

import Exceptions.*;
import constant.CodeConstant;
import constant.MessageConstant;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import result.Result;


/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获所有异常
     */
    @ExceptionHandler(Exception.class)
    public Result ex(Exception exception){
        return Result.error(MessageConstant.SYSTEM_FAILURE, CodeConstant.SYSTEM_FAILURE);
    }

    /**
     * 删除班级时，班级不存在异常
     * @param classNOExistException
     * @return
     */
    @ExceptionHandler(ClassNOExistException.class)
    public Result ClassNOExistException(ClassNOExistException classNOExistException){
        return Result.error("该班级不存在");
    }

    /**
     * 添加学生时，该学生已经存在的异常
     * @param StudentAlreadyExistException
     * @return
     */
    @ExceptionHandler(StudentAlreadyExistException.class)
    public Result StudentAlreadyExistException(StudentAlreadyExistException StudentAlreadyExistException){
        return Result.error("要添加的学生已经存在了",510);
    }


    /**
     * 题目编号重复的异常
     * @param questionNumberExistedException
     * @return
     */
    @ExceptionHandler(QuestionNumberExistedException.class)
    public Result QuestionNumberExistedException(QuestionNumberExistedException questionNumberExistedException){
        return Result.error("编码已经存在",511);
    }

    /**
     * 该试题关联了试卷，不能修改
     * @param answerNoEditException
     * @return
     */
    @ExceptionHandler(AnswerNoEditException.class)
    public Result AnswerNoEditException(AnswerNoEditException answerNoEditException){
        return Result.error("试卷关联了试卷",512);
    }


    /**
     * 编程题的测试用例
     * @param encodeTestNoLessOneException
     * @return
     */
    @ExceptionHandler(EncodeTestNoLessOneException.class)
    public Result EncodeTestNoLessOneException(EncodeTestNoLessOneException encodeTestNoLessOneException){
        return Result.error("编程题的测试用例不能少于一个",513);
    }
}
